package cn.tedu;

import cn.tedu.entity.AccountMessage;
import cn.tedu.service.AccountService;
import cn.tedu.util.JsonUtil;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@RocketMQMessageListener(topic = "orderTopic",consumerGroup = "account-group")
@Component
public class AccountConsumer implements RocketMQListener<String> {

    @Autowired
    private AccountService accountService;

    @Override
    public void onMessage(String json) {
        // json ---> AccountMessage
        AccountMessage am = JsonUtil.from(json, AccountMessage.class);
        accountService.decrease(am.getUserId(), am.getMoney());
    }
}
